package com.wzx.user.controller;


import com.aliyuncs.exceptions.ClientException;
import com.wzx.common.exception.LeadNewsException;
import com.wzx.common.exception.NeedCodeException;
import com.wzx.common.pojo.resp.Result;
import com.wzx.common.util.ValidateUtils;
import com.wzx.user.dto.UpdateUserDto;
import com.wzx.user.dto.UserDto;
import com.wzx.user.service.UserServcie;
import com.wzx.user.vo.RegisterVo;
import com.wzx.user.vo.UserLoginVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @author wzx
 * @version 1.0
 * @date 2022/4/18 - 19:12
 * @description 标题
 */
@Slf4j
@RestController
@RequestMapping("/login")
@Api(tags = "登录管理")
public class LoginController {

    @Autowired
    private UserServcie userServcie;


    @PostMapping("/login")
    @ApiOperation(value = "用户登录")
    public Result<UserDto> login(@RequestBody UserLoginVo userLoginVo) throws Exception {
        UserDto userDto = userServcie.login(userLoginVo);
        return Result.ok(userDto);
    }

    @PostMapping("/loginByUserAmount")
    @ApiOperation(value = "用户登录并且查询用户余额")
    public Result<UserDto> loginByUserAmount(@RequestBody UserLoginVo userLoginVo) throws Exception {
        UserDto userDto = userServcie.loginByUserAmount(userLoginVo);
        return Result.ok(userDto);
    }

    @GetMapping("/getYzm")
    @ApiOperation(value = "获取验证码")
    public Result getYzm(String phone) throws LeadNewsException, ClientException {
        userServcie.getYzm(phone);
        return Result.ok();
    }

    @PostMapping("/register")
    @ApiOperation(value = "用户注册")
    public Result<String> register(@RequestBody RegisterVo registerVo) throws LeadNewsException {
        ValidateUtils.validateFast(registerVo);
        String username = userServcie.register(registerVo);
        return Result.ok(username);
    }

    @PostMapping("/reset")
    @ApiOperation(value = "密码重置")
    public Result<String> reset(@RequestBody RegisterVo registerVo) throws Exception {
        ValidateUtils.validateFast(registerVo);
        String username = userServcie.reset(registerVo);
        return Result.ok(username);
    }

    @PostMapping("/loginByCode")
    @ApiOperation(value = "验证码登录")
    public Result<UserDto> loginByCode(@RequestBody RegisterVo registerVo) throws LeadNewsException, NeedCodeException {
        ValidateUtils.validateFast(registerVo);
        UserDto userDto = userServcie.loginByCode(registerVo);
        return Result.ok(userDto);
    }

    @PostMapping("/updatePwd")
    @ApiOperation(value = "修改密码")
    public Result updatePwd(@RequestBody UpdateUserDto user) throws LeadNewsException {
        userServcie.updatePwd(user);
        return Result.ok();
    }

    @GetMapping("/forwardPwdSendCode")
    @ApiOperation(value = "忘记密码发送验证码")
    public Result forwardPwdSendCode(String phone,Integer userId) throws LeadNewsException, ClientException {
        userServcie.forwardPwdSendCode(phone,userId);
        return Result.ok();
    }

    @PostMapping("/forwardPwd")
    @ApiOperation(value = "忘记密码重新改正")
    public Result forwardPwd(@RequestBody UpdateUserDto updateUserDto) throws LeadNewsException {
        userServcie.forwardPwd(updateUserDto);
        return Result.ok();
    }

    @GetMapping("/logOff")
    @ApiOperation(value = "注释账号")
    public Result logOff(Integer userId) throws LeadNewsException {
        userServcie.logOff(userId);
        return Result.ok();
    }

    @GetMapping("/loginByIphone")
    @ApiOperation(value = "苹果手机快捷登录")
    public Result<UserDto> loginByIphone(String phone) throws LeadNewsException {
        UserDto userDto = userServcie.loginByIphone(phone);
        return Result.ok(userDto);
    }

    @GetMapping("/loginByVisitor")
    @ApiOperation(value = "游客登录")
    public Result<UserDto> loginByVisitor() throws LeadNewsException {
        UserDto userDto = userServcie.loginByVisitor();
        return Result.ok(userDto);
    }

}